package com.wuchao.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuchao.entity.User;
import com.wuchao.exception.BusinessException;
import com.wuchao.mapper.UserMapper;
import com.wuchao.response.ResultCode;
import com.wuchao.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuchao.sjwtutils.JWTToken;
import com.wuchao.sjwtutils.JWTUtils;
import com.wuchao.sjwtutils.MD5Utils;
import com.wuchao.vo.UserVO;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author wuchao
 * @since 2023-12-12
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public Page<User> findUserList(Page<User> userPage, UserVO userVO) {
        List<User> userList = this.baseMapper.findUserList(userPage, userVO);
        userPage.setRecords(userList);
        return userPage;
    }
    //用流去导出我们的泛型
    @Override
    public void exportUserExcel(HttpServletResponse response, List<User> tbUsers) throws IOException {
        //----- 写入excel文件
        String exportFileName = "TbUserInfo.xlsx";
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.ms-excel");
        response.addHeader("Content-Disposition", "attachment;filename=" + exportFileName);
        EasyExcel.write(response.getOutputStream(), User.class)
                .sheet("用户信息167")
                //自动列宽
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                .doWrite(tbUsers);
    }

    @Override
    public User loadUserByUsername(String username) {
        return this.baseMapper.loadUserByUsername(username);
    }

    @Override
  // @Transactional
    public String login(String username, String password) {
        //1拿你的账户去看一下有没有
        User user = this.baseMapper.loadUserByUsername(username);
        if(user==null){
            throw new BusinessException(ResultCode.USER_ACCOUNT_NOT_EXIST.getCode(),
                    ResultCode.USER_ACCOUNT_NOT_EXIST.getMessage());
        }
        //拿到老密码和盐
        String oldpwd=user.getPassword();
        String salt=user.getSalt();
        //加盐
        String newpwd = MD5Utils.md5Encryption(password, salt);
        if(!oldpwd.equals(newpwd)){
            throw new BusinessException(ResultCode.USER_CREDENTIALS_ERROR .getCode(),
                    ResultCode.USER_CREDENTIALS_ERROR .getMessage());
        }
        //发牌
//        JWTUtils.sign(username,newpwd);
//        //后台存着牌
        //发token
        String token = JWTUtils.sign(username, newpwd);
        //接受token，并保存，然后开始整套流程验证
        JWTToken jwtToken = new JWTToken(token);
        try {//securityManager
            SecurityUtils.getSubject().login(jwtToken);
        } catch(BusinessException B){
            throw new BusinessException(ResultCode.NO_PERMISSION.getCode(),
                    ResultCode.NO_PERMISSION.getMessage());
        }
        return token;//黑前端的 jwt


    }
}
